<template>
  <a-modal
    title="设置固定车位"
    :visible="visible"
    :width="800"
    :mask-closable="false"
    :confirmLoading="loading"
    @cancel="hide"
    @ok="ok"
  >
    <a-form-model
      ref="form"
      :model="form"
      :rules="rules">
      <a-row :gutter="formLayout.gutter">
        <a-col :span="formLayout.span12">
          <a-form-model-item label="车位编号" prop="code">
            <a-input disabled v-model="form.code" :maxLength="2" placeholder="格式：A1~A9、B1~B9、..." />
          </a-form-model-item>
        </a-col>
        <a-col :span="formLayout.span12">
          <a-form-model-item label="车位是否空闲" prop="idle">
            <a-select disabled v-model="form.idle" placeholder="请选择" :options="idleOptions" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="formLayout.gutter">
        <a-col :span="formLayout.span24">
          <a-form-model-item label="固定车位用户" prop="userId">
            <a-select v-model="form.userId" placeholder="请选择">
              <a-select-option
                v-for="user in userList"
                :key="user.uuid"
                :value="user.uuid">
                <div class="flex-h-hs-vc">
                  <div>
                    {{ user.realname }}
                    <span v-if="user.phone">（{{ user.phone }}）</span>
                    &nbsp;
                  </div>
                </div>
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="formLayout.gutter">
        <a-col :span="formLayout.span12">
          <a-form-model-item label="车牌号码" prop="number">
            <!-- TODO: 可以考虑下拉选择形式 -->
            <a-input v-model="form.number" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-modal>
</template>

<script>
import { getEasyUserListAPI } from '@/api/user.js'

let originForm = null

export default {
  data () {
    return {
      idleOptions: [
        { label: '空闲', value: 0 },
        { label: '占用', value: 1 }
      ],
      visible: false,
      loading: false,
      form: {
        code: '',
        idle: 0,
        userId: undefined,
        number: ''
      },
      rules: {
        userId: { required: true, message: '请选择固定车位用户', trigger: 'change' },
        number: { required: true, message: '请输出车牌号码', trigger: 'change' }
      },
      cb: null,
      userList: []
    }
  },
  created () {
    originForm = this.deepClone(this.form)
    getEasyUserListAPI().then(res => {
      this.userList = res.data.rows
    })
  },
  methods: {
    open (form, cb) {
      this.cb = cb
      if (form) {
        for (const key in this.form) {
          if (key === 'userId' && !form[key]) {
            this.form.userId = undefined
          } else {
            this.form[key] = form[key]
          }
        }
      }
      this.visible = true
    },
    hide (close = true) {
      if (close) {
        this.form = this.deepClone(originForm)
        this.loading = false
        this.visible = false
      } else {
        this.loading = false
      }
    },
    ok () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          this.cb && this.cb(this.form, this.hide)
        }
      })
    }
  }
}
</script>
